<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <base href="#(basePath)"/>
  <title>角色管理 - 分配权限</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <style type="text/css">
  .panel h1{
  	font-size: 16px;
  	font-weight:bold;
    height: 28px;
    padding: 0 5px;
    line-height: 28px;
  }
  </style>
  <link rel="stylesheet" href="static/layui/css/layui.css" media="all">
</head>
<body>
<form action="">
	<input type="hidden" name="adminRole.id" value="#(adminRole.id)" >
	<input type="hidden" value="#(adminRole.sourceUrl)" id="sourceUrl">
	<input type="hidden" value="#(adminRole.sourceGroup)" id="sourceGroup">
	<div class="layui-card">
		<div class="layui-card-header">当前角色：#(adminRole.roleName)</div>
        <div class="layui-card-body" style="background-color: #F2F2F2;">
		#for(item : sourceList)
			#if(item.parent == "0")
			<div class="panel" id="#(item.value)">
				<h1><input class="first_menu" type="checkbox" name="sourceGroup" value="#(item.value)">#(item.name)</h1>
				<div>
					<table class="sourceTable" style="margin-left:5px">
					#if(item.childs != null)
						#for(second : item.childs)
						<tr>
							<td class="second_menu_td">
								<input class="second_menu" type="checkbox"  name="sourceUrl" value="#(second.value)"/>#(second.name)
							</td>
							<td class="btn_td">
							#if(second.childs != null)
								-
								#for(third : second.childs)
								<input class="third_menu" type="checkbox"  name="sourceUrl" value="#(third.value)"/>#(third.name)
								#end
							#end
							</td>
						</tr>
						#end
					#end
					</table>
				</div>
			</div>
			#end
		#end
		</div>
	</div>
	<div class="layui-form-item layui-hide">
		<input type="button" lay-submit lay-filter="layuiadmin-app-form-edit" id="layuiadmin-app-form-edit" value="确认编辑">
	</div>
</form>
  <script src="static/layui/layui.js"></script>
  <script>
  layui.config({
	base: '#(basePath)' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'form'], function(){
    var $ = layui.$;
	///========= 选项框选择相关处理[START] =========///
	var $ = layui.$;
	var sourceUrl = $("#sourceUrl").val();
	if(sourceUrl != ""){
		var urlArray = sourceUrl.split(",");
		for(var i = 0; i < urlArray.length;i++){
			var ckbCtr = $("input[value='"+urlArray[i]+"']");
			if(ckbCtr){
				ckbCtr.prop("checked",true);
			}
		}		
	}
	var sourceGroup = $("#sourceGroup").val();
	if(sourceGroup != ""){
		var urlArray = sourceGroup.split(",");
		for(var i = 0; i < urlArray.length;i++){
			var ckbCtr = $("input[value='"+urlArray[i]+"']");
			if(ckbCtr){
				ckbCtr.prop("checked",true);
			}
		}		
	}
	
	/*--- 初始化选中事件设置] ---*/
	$(".first_menu").off("click").on("click",function(){
		var $Checked = $(this).prop("checked");
		var val = $(this).val();
		var senondMenu = $("#" + val).find(".second_menu");
		var thirdMenu = $("#" + val).find(".third_menu");
		$(senondMenu).each(function(){
			$(this).prop("checked",$Checked);
		});
		$(thirdMenu).each(function(){
			$(this).prop("checked",$Checked);
		});
	});

	$(".second_menu").off("click").on("click",function(){
		var $Checked = $(this).prop("checked");
		var firstMenum = $(this).parents(".panel").find(".first_menu");
		if($Checked){
			firstMenum.prop("checked",true);
		}else{
			//统计2级菜单是否全部取消选择，若是一级菜单取消
			 var secondMenu = $(this).parents(".sourceTable").find(".second_menu");
			 var c = 0;
			 $(secondMenu).each(function(){
				if($(this).prop("checked")){
					c ++;
				}
			 });
			 if(c == 0){
				 firstMenum.prop("checked",false);
			 }
		}
		
		var thirdMenu = $(this).parent().next().find(".third_menu");
		$(thirdMenu).each(function(){
			$(this).prop("checked",$Checked);
		});
		
	});

	$(".third_menu").off("click").on("click",function(){
		var $Checked = $(this).prop("checked");
		var secondMenu = $(this).parent().prev().find(".second_menu");
		if($Checked){
			secondMenu.prop("checked",true);
		}
		
		//统计2级是否全部取消选择，若是1级菜单取消
		var allSecondMenu = $(this).parents(".sourceTable").find(".second_menu");
		var count = 0;
		 $(allSecondMenu).each(function(){
			if($(this).prop("checked")){
				count ++;
			}
		 });
		 if(count == 0){
			 $(this).parents(".panel").find(".first_menu").prop("checked",false);
		 }else{
			 $(this).parents(".panel").find(".first_menu").prop("checked",true);
		 }
		
	});
	///========= 选项框选择相关处理[END] =========///
    $("#layuiadmin-app-form-edit").on("click", function(){
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引  
        $.ajax({
      	  url: layui.setter.base+"admin/role/saveAlloctSource",
      	  type:"post",
      	  data: $("form").serialize(),
      	  success: function(res){
  			  if(res.code !== 0){
  				parent.layer.msg(res.msg, {icon: 2});
  			  }else{
  	    		  parent.layer.msg(res.msg, {icon: 1});
  	    	      parent.layer.close(index); //再执行关闭 
  			  }
      	  },
      	  error: function(xhr){parent.layer.msg("出错了" + xhr);}
        });
    });
  })
  </script>
</body>
</html>
